const pool = require("./db.js");

class ReadedDao {
    async addReaded(noticeID, userID) {
        const [rows, fields] = await pool.execute(
            "INSERT INTO Readed (notice_id, user_id, readed_ifReaded) VALUES (?, ?, ?)",
            [noticeID, userID, 0]
        );
        return rows;
    }

    async deleteReadedByNoticeIdAndUserId(noticeId,userId){
        const [rows, fields] = await pool.execute(
            "DELETE FROM Readed WHERE notice_id = ? AND user_id = ?",
            [noticeId, userId]
        );
        return rows;
    }

    async deleteReadedByNoticeId(noticeId){
        const [rows, fields] = await pool.execute(
            "DELETE FROM Readed WHERE notice_id = ?",
            [noticeId]
        );
        return rows;
    }

    async delReaded(ID) {
        const [rows, fields] = await pool.execute(
            "DELETE FROM Readed WHERE id = ?",
            [ID]
        );
        return rows;
    }

    async selectReadedsByNoticeID(notice_id){
        const [rows, fields] = await pool.execute(
            "SELECT DISTINCT * FROM Readed WHERE notice_id = ?",
            [notice_id]
        );
        return rows;
    }

    async selectReadedByNoticeID(noticeID) {
        const [rows, fields] = await pool.execute(
            "SELECT DISTINCT * FROM Readed WHERE notice_id = ? And readed_ifReaded = ?",
            [noticeID, 1]
        );
        return rows;
    }

    async selectUnReadedByNoticeID(noticeID) {
        const [rows, fields] = await pool.execute(
            "SELECT DISTINCT * FROM Readed WHERE notice_id = ? And readed_ifReaded = ?",
            [noticeID, 0]
        );
        return rows;
    }

    async selectReadedByNoticeIDAndUserID(noticeId,userId){
        const [rows, fields] = await pool.execute(
            "SELECT DISTINCT * FROM Readed WHERE notice_id = ? AND user_id = ?",
            [noticeId, userId]
        );
        return rows;
    }

    async updateReaded(noticeID, userID, ifReaded) {
        const [rows, fields] = await pool.execute(
            "UPDATE Readed SET readed_ifReaded = ? WHERE notice_id = ? AND user_id = ?",
            [ifReaded, noticeID, userID]
        );
        return rows;
    }
}

module.exports = new ReadedDao();